package com.ychong.library.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.view.animation.Animation
import android.view.animation.ScaleAnimation




/**
 * @ProjectName: ctq-eqc-app
 * @Package: com.ctq.eqc.weight.customview
 * @ClassName: WaveViewNew
 * @Description: 波纹渐变view
 * @Author: msc
 * @CreateDate: 2021/4/28 10:52
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/4/28 10:52
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
class SpreadView @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null
) : View(context, attrs) {
    private var waveColor = 0
    private val waveCount = 3
    private var paint: Paint? = null
    private var mWidth = 0
    private var mHeight = 0
    private var centerX = 0
    private var centerY = 0
    private var radius = 0f// 最外圆半径，即最大半径
    private var innerRadius = 0f// 最内圆的半径，即最小半径
    private val waveDegreeArr = FloatArray(waveCount)
    private val isRunning = true

    private val scaleArr = arrayOf(0,3,5,8)
    private var curScale = 0
    private val defaultV = dp2Px(10)

    private val mShieldPaint = Paint()

    init {
        val animation: Animation = ScaleAnimation(1f, 1f, 2f,2f, 3, 3f, 4,4f)
        animation.setDuration(1500) //动画时间

        animation.setRepeatCount(3) //动画的反复次数

        animation.setFillAfter(true) //设置为true，动画转化结束后被应用

        this.startAnimation(animation) //開始动画


    }
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mWidth = w
        mHeight = h
        centerX = mWidth / 2
        centerY = mHeight / 2
        radius = Math.min(mWidth, mHeight) / 2f
        innerRadius = 120f
        for (i in 0 until waveCount) {
            waveDegreeArr[i] = innerRadius + (radius - innerRadius) / waveCount * i
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var widthMeasureSpec = widthMeasureSpec
        var heightMeasureSpec = heightMeasureSpec
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        if (widthMode == MeasureSpec.UNSPECIFIED || widthMode == MeasureSpec.AT_MOST) {
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(
                dp2Px(120),
                MeasureSpec.EXACTLY
            )
        }
        if (heightMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.AT_MOST) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(
                dp2Px(120),
                MeasureSpec.EXACTLY
            )
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onDraw(canvas: Canvas) {
        //drawWave(canvas)
        drawShield(canvas)
    }

    private fun drawShield(canvas: Canvas) {

        mShieldPaint.strokeWidth = 3f
        mShieldPaint.style = Paint.Style.STROKE
        mShieldPaint.color = Color.GREEN
        val path = Path()
        val centerX = width/2f
        val centerY = height/2f
        for (i in 0..2){
            val spaceH:Int = i*30
            val spaceW:Int = i*22

            path.moveTo(centerX, (centerY-dp2Px(240)-defaultV*curScale) + dp2Px(spaceH))
            path.quadTo(
                (centerX - dp2Px(50))-defaultV*curScale + dp2Px(spaceW),
                (centerY - dp2Px(180))-defaultV*curScale + dp2Px(spaceW),
                (centerX - dp2Px(150))-defaultV*curScale + dp2Px(spaceW),
                (centerY - dp2Px(160))-defaultV*curScale + dp2Px(spaceW)
            )
            canvas.drawPath(path, mShieldPaint)

            canvas.drawLine(
                (centerX - dp2Px(150))-defaultV*curScale + dp2Px(spaceW),
                (centerY - dp2Px(160))-defaultV*curScale + dp2Px(spaceW),
                (centerX - dp2Px(150))-defaultV*curScale + dp2Px(spaceW),
                (centerY + dp2Px(60))+defaultV*curScale - dp2Px(spaceW),
                mShieldPaint
            )

            path.moveTo(
                (centerX - dp2Px(150))-defaultV*curScale + dp2Px(spaceW),
                (centerY + dp2Px(60))+defaultV*curScale - dp2Px(spaceW)
            )
            path.quadTo(
                (centerX - dp2Px(80))-defaultV*curScale + dp2Px(spaceW),
                (centerY + dp2Px(150))+defaultV*curScale - dp2Px(spaceW),
                centerX,
                (centerY + dp2Px(200))+defaultV*curScale - dp2Px(spaceH)
            )
            canvas.drawPath(path, mShieldPaint)

            //右边
            path.moveTo(centerX, (centerY-dp2Px(240)-defaultV*curScale)+ dp2Px(spaceH))
            path.quadTo(
                (centerX +dp2Px(50))+defaultV*curScale - dp2Px(spaceW),
                centerY - dp2Px(180)-defaultV*curScale + dp2Px(spaceW),
                (centerX + dp2Px(150))+defaultV*curScale - dp2Px(spaceW),
                (centerY - dp2Px(160))-defaultV*curScale + dp2Px(spaceW)
            )
            canvas.drawPath(path, mShieldPaint)

            canvas.drawLine(
                (centerX + dp2Px(150))+defaultV*curScale - dp2Px(spaceW),
                (centerY - dp2Px(160))-defaultV*curScale + dp2Px(spaceW),
                (centerX +dp2Px(150))+defaultV*curScale - dp2Px(spaceW),
                (centerY + dp2Px(60))+defaultV*curScale - dp2Px(spaceW),
                mShieldPaint
            )

            path.moveTo(
                (centerX + dp2Px(150))+defaultV*curScale - dp2Px(spaceW),
                (centerY + dp2Px(60))+defaultV*curScale - dp2Px(spaceW)
            )
            path.quadTo(
                (centerX + dp2Px(90))+defaultV*curScale - dp2Px(spaceW),
                (centerY + dp2Px(150))+defaultV*curScale -dp2Px(spaceW),
                centerX,
                (centerY + dp2Px(200))+defaultV*curScale - dp2Px(spaceH)
            )
            canvas.drawPath(path, mShieldPaint)
        }

//        for (item in scaleArr){
//            if (curScale!=item){
//                curScale=item
//                break
//            }
//        }
//        postInvalidateDelayed(500)
    }

    private fun drawWave(canvas: Canvas) {
        for (i in 0 until waveCount) {
            paint!!.alpha = (255 - 255 * waveDegreeArr[i] / radius).toInt()
            canvas.drawCircle(centerX.toFloat(), centerY.toFloat(), waveDegreeArr[i], paint!!)
        }
        for (i in waveDegreeArr.indices) {
            waveDegreeArr[i] = waveDegreeArr[i] + 4
            if (waveDegreeArr[i] > radius) {
                waveDegreeArr[i] = innerRadius
            }
        }
        if (isRunning) {
            postInvalidateDelayed(200)
        }
    }

    private fun dp2Px(dpValue: Int): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dpValue.toFloat(),
            resources.displayMetrics
        ).toInt()
    }

    companion object {
        private const val TAG = "WaveView"
    }

    init {
        waveColor = -0x1
        paint = Paint(Paint.ANTI_ALIAS_FLAG)
        paint!!.color = waveColor
        paint!!.style = Paint.Style.FILL
    }


}